#include "mhdef.h"
#include "ccycle.h"
#include <assert.h>
#include <string.h>
#include "globe.h"

void ccycle_init(CCYCLE *cycle)
{
    assert(cycle != NULL);

    memset(cycle, 0, sizeof (CCYCLE));

    cycle->state = CYCLE_STOP;
}

void ccycle_exec(CCYCLE *cycle)
{
    assert(cycle != NULL);

    cycle->index++;
    if (cycle->index > cycle->index_end)
    {
        cycle->index = cycle->index_start;

        cycle->group++;
        if (cycle->group > cycle->group_end)
        {
            cycle->group = cycle->group_start;
        }
    }
}

/*
 * @note: 只循环增加index
 */
void ccycle_exec_index(CCYCLE *cycle)
{
    assert(cycle != NULL);

    cycle->index++;
    if (cycle->index > cycle->index_end)
    {
        cycle->index = cycle->index_start;
    }
}

CCYCLE_STATE ccycle_get_state(CCYCLE *cycle)
{
    assert(cycle != NULL);

    return cycle->state;
}

void ccycle_set_state(CCYCLE *cycle, CCYCLE_STATE state)
{
    assert(cycle != NULL);

    cycle->state = state;
}

void ccycle_stop(CCYCLE *cycle)
{
    assert(cycle != NULL);

    cycle->state = CYCLE_STOP;
}

short ccycle_get_group(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->group;
}

short ccycle_get_start_group(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->group_start;
}

short ccycle_get_end_group(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->group_end;
}

void ccycle_set_group(CCYCLE *cycle, short group)
{
    assert(cycle != NULL);
    cycle->group = group;
}

void ccycle_set_start_group(CCYCLE *cycle, short group_start)
{
    assert(cycle != NULL);
    cycle->group_start = group_start;
}

void ccycle_set_end_group(CCYCLE *cycle, short group_end)
{
    assert(cycle != NULL);
    cycle->group_end = group_end;
}


void ccycle_set_index(CCYCLE *cycle, short index)
{
    assert(cycle != NULL);
    cycle->index = index;
}

void ccycle_set_start_index(CCYCLE *cycle, short index_start)
{
    assert(cycle != NULL);
    cycle->index_start = index_start;
}

void ccycle_set_end_index(CCYCLE *cycle, short index_end)
{
    assert(cycle != NULL);
    cycle->index_end = index_end;
}

short ccycle_get_index(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->index;
}

short ccycle_get_start_index(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->index_start;
}

short ccycle_get_end_index(CCYCLE *cycle)
{
    assert(cycle != NULL);
    return cycle->index_end;
}

/*
 * @note: 判断一个循环是否完成
 */
BOOL ccycle_complete(CCYCLE *cycle)
{
    assert(cycle != NULL);

    if (cycle->group == cycle->group_start)
    {
        if (cycle->index == cycle->index_start)
        {
            return TRUE;
        }
    }

    return FALSE;
}
